package com.mflex.mpmmic.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.mflex.mpmmic.R;
import com.mflex.mpmmic.model.CustomApiResult;
import com.mflex.mpmmic.model.PdaLink;
import com.mflex.mpmmic.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText txtEmployee;
    private EditText txtGlue;
    private EditText txtLocation;
    private EditText txtModule;

    private Button btnClear;
    private Button btnExit;

    private static String userNmae;


    MiniLoadingDialog mMiniLoadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载中...");
        txtEmployee = (EditText) findViewById(R.id.txtEmployee);
        txtGlue = (EditText) findViewById(R.id.txtGlue);
        txtLocation = (EditText) findViewById(R.id.txtLocation);
        txtModule = (EditText) findViewById(R.id.txtModule);
        btnClear = (Button) findViewById(R.id.btnClear);
        btnExit = (Button) findViewById(R.id.btnExit);

        Intent intent = getIntent();
        userNmae = intent.getStringExtra("userName");

        txtEmployee.requestFocus();

        txtEmployee.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
                    if(StringUtils.isEmpty(txtEmployee.getText().toString())){
                        txtEmployee.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtEmployee.requestFocus();
                            }
                        },200);
                        return false;
                    }
                    txtGlue.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            txtGlue.requestFocus();
                        }
                    },200);
                }
                return false;
            }
        });

        txtGlue.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
                    if(StringUtils.isEmpty(txtGlue.getText().toString())){
                        txtGlue.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtGlue.requestFocus();
                            }
                        },200);
                        return false;
                    }
                    if(StringUtils.isEmpty(txtEmployee.getText().toString())){
                        new MaterialDialog.Builder(view.getContext())
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content("操作员未输入")
                                .positiveText(R.string.lab_submit)
                                .show();
                        txtEmployee.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtEmployee.requestFocus();
                            }
                        },200);
                        return false;
                    }
                    txtLocation.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            txtLocation.requestFocus();
                        }
                    },200);
                }
                return false;
            }
        });

        txtLocation.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
                    if(StringUtils.isEmpty(txtLocation.getText().toString())){
                        txtLocation.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtLocation.requestFocus();
                            }
                        },200);
                        return false;
                    }
                    if(StringUtils.isEmpty(txtGlue.getText().toString())){
                        new MaterialDialog.Builder(view.getContext())
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content("胶水未输入")
                                .positiveText(R.string.lab_submit)
                                .show();
                        txtGlue.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtGlue.requestFocus();
                            }
                        },200);
                        return false;
                    }
                    txtModule.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            txtModule.requestFocus();
                        }
                    },200);
                }
                return false;
            }
        });

        txtModule.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
                    if(StringUtils.isEmpty(txtModule.getText().toString())){
                        txtModule.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtModule.requestFocus();
                            }
                        },200);
                        return false;
                    }
                    if(StringUtils.isEmpty(txtLocation.getText().toString())){
                        new MaterialDialog.Builder(view.getContext())
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content("库位编号未输入")
                                .positiveText(R.string.lab_submit)
                                .show();
                        txtLocation.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtLocation.requestFocus();
                            }
                        },200);
                        return false;
                    }

                    request(view);
                }
                return false;
            }
        });

        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txtEmployee.setText("");
                txtGlue.setText("");
                txtLocation.setText("");
                txtModule.setText("");
                txtEmployee.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        txtEmployee.requestFocus();
                    }
                },200);
            }
        });
    }

    private void request(View v) {
        mMiniLoadingDialog.show();
        PdaLink pdaLink = new PdaLink();
        pdaLink.setOperator(userNmae);
        pdaLink.setGlue(txtGlue.getText().toString());
        pdaLink.setLocation(txtLocation.getText().toString());
        pdaLink.setModule(txtModule.getText().toString());

        XHttp.post("api/NPMMic/PDALink")
                .upJson(JsonUtil.toJson(pdaLink))
                .syncRequest(false)
                .onMainThread(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String response) {
                        mMiniLoadingDialog.dismiss();
                        XToastUtils.success("提交成功");
                        txtEmployee.setText("");
                        txtGlue.setText("");
                        txtLocation.setText("");
                        txtModule.setText("");
                        txtEmployee.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                txtEmployee.requestFocus();
                            }
                        },200);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        new MaterialDialog.Builder(v.getContext())
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                }) {
                });
    }
}